using System.Collections;
using System.Collections.Generic;
using Game.Framework;
using Game.Framework.Localization;
using Game.Framework.UI;
using UnityEngine;
using UnityEngine.UI;

namespace Game.Applcation
{
    public class UILaunching : UIView
    {
        
        private LocalizeTextMeshProUGUI _loadingText;
        private Slider _progressSlider;

        private float _progress;
        private float _targetProgress;
        
        protected override void OnCreate()
        {
            base.OnCreate();

            _loadingText = BindItem<LocalizeTextMeshProUGUI>("Root/BottomGroup/SliderLoading/TextLoad");
            _progressSlider = BindItem<Slider>("Root/BottomGroup/SliderLoading");
        }
        
        public static void Open()
        {
#if UNITY_ANDROID
            UIManager.Instance.Open<UILaunching>("UI/UILaunching/Prefabs/UILaunching");
#else
             UIManager.Instance.Open<UILaunching>("UI/UILaunching/Prefabs/UILaunchingIOS");
#endif
            
            
        }


        

        protected override void OnOpen<T>(UIData data)
        {
            base.OnOpen<T>(data);

            SetProgress(0);
            EventBus.Register<EventLaunchLoadingProgress>(OnEventUpdateLoadingProgress);
        }
        
        protected override void OnClose()
        {
            base.OnClose();
            EventBus.UnRegister<EventLaunchLoadingProgress>(OnEventUpdateLoadingProgress);
        }
        
        private void OnEventUpdateLoadingProgress(EventLaunchLoadingProgress e)
        {
            _targetProgress = Mathf.Min(10f, e.progress);
            _stateMachine.SetState<UILaunchingStateProgress>(new UIStateData(this));
        }
        
        public void SetProgress(float lerpProgress)
        {
            _progress = Mathf.Lerp(_progress, _targetProgress, lerpProgress);
            _progressSlider.value = _progress / 10f;
            
            var str = Mathf.Floor(_progressSlider.value * 100);
            string strValue = $"{str} %";
            _loadingText.SetText(strValue);
        }
    }
}


